﻿--[[
   @brief    CTips广告(进助手页和游戏退出时，右下角弹窗)
   @author   hanmanhuang
   @version  1.0
   @date     2017-10-19
--]]

CTips_control = {
    tips_request_url = LoadStr("CTIPS_REQ_URL");
    tips_report_url = LoadStr("CTIPS_REPORT_URL");

    before_tips_pos_id = "8001";
    after_tips_pos_id = "8002";
    
    tips_info_list = {};
    cur_tips_id = 0;

    pre_banner_game_id = 0;

    is_game_running = false;
    language = nil;

    init = function()
        CTips_control.inner_log("init");
        ierd_tgp.tpf_ui.Require("CTips\\view.lua");
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[CTips_control] %s", msg), core_msg);
    end;
    
    -- banner节点切换
    OnBannerItemChange = function(event_id, json_str)
        CTips_control.inner_log("OnBannerItemChange : "..json_str);
        local param = base_utility.json_to_table(json_str);
        local game_id = 0;

        if param ~= nil and param.module_id ~= nil then
            game_id = tonumber(param.module_id);
        end;

        if 0 ~= pre_banner_game_id and pre_banner_game_id == game_id then
            CTips_control.inner_log("click same game banner, do not request tips");
            return;
        end;
        pre_banner_game_id = game_id;

        if nil == game_id or GAME_ID.INVALID_GAME_ID == game_id or 
           GAME_ID.GAEM_ID_HOME_PAGE == game_id or 
           GAME_ID.GAME_ID_SHOP == game_id or 
           GAME_ID.GAME_ID_GAME_LIVE == game_id or
           GAME_ID.GAME_ID_NETBAR == game_id or 
           GAME_ID.GAME_ID_TESTZONE == game_id or
           GAME_ID.GAME_ID_CHINATOWN == game_id then
            CTips_control.inner_log("not real game, do not request tips");
            return;
        end;

        if CTips_control.is_game_running then
            CTips_control.inner_log("game is running, do not request tips");
            return;
        end;

        CTips_control.RequestGameTips(game_id, CTips_control.before_tips_pos_id);
    end;

    NotifyGameRunning = function(event_id, param)
        CTips_control.inner_log("NotifyGameRunning : "..param);
        local tb_game_state = base_utility.json_to_table(param);
        if tb_game_state ~= nil and tb_game_state.running_state ~= nil then
            if tb_game_state.running_state > 0 then
                CTips_control.is_game_running = true;
            else
                CTips_control.is_game_running = false;
            end;
        end;
    end;
    
    -- 游戏开始    
    OnGameStart = function(event_id, json_str)
        CTips_control.inner_log("OnGameStart : "..json_str);
        CTips_view.Close();
    end;

    -- 游戏结束
    OnGameEnd = function(event_id, json_str)
        CTips_control.inner_log("OnGameEnd : "..json_str);
        local json_tb = base_utility.json_to_table(json_str);
        if json_tb ~= nil and json_tb.game_id ~= nil and 
           nil ~= tonumber(json_tb.game_id) and GAME_ID.INVALID_GAME_ID ~= tonumber(json_tb.game_id) then
            CTips_control.RequestGameTips(json_tb.game_id, CTips_control.after_tips_pos_id);
        end;
    end;

    RequestGameTips = function(game_id, pos_id)
        if CTips_control.language == nil then
            CTips_control.language = base_utility.get_currrent_language_str();
        end;
        local biz_ticket = "";
        local biz_tbl = comm_center.call_service("Service_GetRelatedTicket", {});
        if biz_tbl ~= nil and biz_tbl.biz_ticket ~= nil then
            biz_ticket = biz_tbl.biz_ticket;
        end

        local custom_cookie = {};
        custom_cookie["language"] = CTips_control.language;
        custom_cookie["tgp_user_type"] = TLoginModule.GetLoginAccountType();
        custom_cookie["tgp_biz_ticket"] = biz_ticket;
    
        local req_data = {};
        req_data["game_id"] = tonumber(game_id);
        req_data["pos_id"] = tostring(pos_id);
        
        local url = CTips_control.tips_request_url .."?p="..base_utility.table_to_json(req_data);
        --url = web_utility.make_login_status_url(url, TLoginModule.GetLoginUin());
        base_utility.curl_request_async(url, "CTips_control.OnGetGameTips", true, nil, true, nil, nil, custom_cookie);

        CTips_control.inner_log("RequestGameTips : "..url);
    end;

    OnGetGameTips = function(json_str)
        CTips_control.inner_log("OnGetGameTips : "..json_str);
        local res_tb = base_utility.json_to_table(json_str);
        if nil == res_tb or nil == res_tb.result or nil == res_tb.data or 
           nil == res_tb.data.child_list or type(res_tb.data.child_list) ~= "table" or
           nil == res_tb.data.child_list[1] or nil == res_tb.data.child_list[1].ads_info or
           nil == res_tb.data.child_list[1].child_pos_id then
            return;
        end;

        if res_tb.result == 0 then
            local tips_info = {};
            tips_info.ads_info = res_tb.data.child_list[1].ads_info;
            tips_info.pos_id = res_tb.data.child_list[1].child_pos_id;
            if tips_info.pos_id == CTips_control.before_tips_pos_id then
                CTips_control.ShowBeforeTips(tips_info);
            elseif tips_info.pos_id == CTips_control.after_tips_pos_id then
                CTips_control.ShowAfterTips(tips_info);
            end;
        end;
    end;
    
    -- 显示登录前广告
    ShowBeforeTips = function(tips_info)
        CTips_control.inner_log("ShowBeforeTips");
        CTips_view.ShowCTips(tips_info);
    end;
    
    -- 显示退出后广告
    ShowAfterTips = function(tips_info)
        CTips_control.inner_log("ShowAfterTips");
        CTips_view.ShowCTips(tips_info);
    end;

    ReportGameTipsShowed = function()
        CTips_control.inner_log("ReportGameTipsShowed");
        local tips_info = CTips_control.tips_info_list[CTips_control.cur_tips_id];
        if nil == tips_info or nil == tips_info.ads_info or nil == tips_info.pos_id then
            CTips_control.inner_log("get tips_info nil");
            return;
        end;

        local req_data = {};
        req_data["expose_info_list"] = {};
        local expose_data = {};
        expose_data.task_id = tips_info.ads_info.task_id;
        expose_data.pos_id = tips_info.pos_id;
        expose_data.ad_id = tips_info.ads_info.ad_id;
        expose_data.game_id = tips_info.ads_info.game_id;
        req_data.expose_info_list[1] = expose_data;

        
        local url = CTips_control.tips_report_url .."?p="..base_utility.table_to_json(req_data);
        --url = web_utility.make_login_status_url(url, TLoginModule.GetLoginUin());
        base_utility.curl_request_async (url, "CTips_control.OnReportGameTips", true, nil, true);
        
        CTips_control.inner_log("send report : "..url);
    end;

    OnReportGameTips = function(json_str)
        CTips_control.inner_log("OnReportGameTips : "..json_str);
    end;
};

CTips_control.init();